package zhou.cutfruit.view;

import java.util.ArrayList;
import java.util.Random;

import zhou.cutfruit.FruitActivity;
import zhou.cutfruit.R;
import zhou.cutfruit.models.Movable;
import zhou.cutfruit.thread.DrawThread;
import zhou.cutfruit.util.ConstantNumber;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

	//这些其实都应该设置成全局变量，用单例模式调用
	ArrayList<Bitmap> fruitBitmaps = new ArrayList<Bitmap>();// 用于维护水果的图片的集合
	ArrayList<Bitmap> waterMelons = new ArrayList<Bitmap>();//用于维护西瓜图片的集合
	Bitmap bgBitmap;// 背景图
	Bitmap missCountBitmap;// 漏掉一个西瓜的 ×
	Bitmap[] jamBitmaps;// 果酱图片

	ArrayList<Movable> fruits = new ArrayList<Movable>();// 用于维护水果对象的集合
	DrawThread drawThread;// 用于动态绘制GameView的线程


	int height;// 在surfaceView的Create里获得动画屏幕的高
	int width; // 获得宽

	



	boolean isGameOver = false;//游戏结束了吗

	Activity activity; //传进来的activity，最后证实没有必要


	Paint paint; //画笔
	private Path path; //路径（从Activity中OnTouchListener Move事件传入）

	/**
	 * 初始化GameView
	 * @param activity 调用gameView的Avtivity
	 */
	public GameView(Activity activity) {
		super(activity);
		getHolder().addCallback(this);
		innitBitmaps(getResources());
		innitMovables();
		innitPaint();
		drawThread = new DrawThread(this, this.getHolder());
		this.activity = activity;

	
	}
//初始化画笔
	void innitPaint() {
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.FILL);
		paint.setStrokeWidth(5);
		paint.setAntiAlias(true);
	}
//初始化图片
	void innitBitmaps(Resources r) {
		fruitBitmaps.add(BitmapFactory
				.decodeResource(r, R.drawable.wartermelon));
		fruitBitmaps.add(BitmapFactory.decodeResource(r,
				R.drawable.wartermelon_cuted));
		fruitBitmaps.add(BitmapFactory.decodeResource(r,
				R.drawable.wartermelon_cuted_right));
		
		
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon));
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon_45));
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon_90));
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon_135));
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon_180));
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon_225));
		waterMelons.add(BitmapFactory.decodeResource(r, R.drawable.wartermelon_315));
		

		jamBitmaps = new Bitmap[10];
		jamBitmaps[0] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_01);
		jamBitmaps[1] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_02);
		jamBitmaps[2] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_03);
		jamBitmaps[3] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_04);
		jamBitmaps[4] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_05);
		jamBitmaps[5] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_06);
		jamBitmaps[6] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_07);
		jamBitmaps[7] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_08);
		jamBitmaps[8] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_09);
		jamBitmaps[9] = BitmapFactory.decodeResource(r,
				R.drawable.juice_fly_red_10);

		bgBitmap = BitmapFactory.decodeResource(r, R.drawable.wood2);
	}


//初始化水果
	void innitMovables() {

		Random random = new Random();
		Movable fruit = new Movable(
				random.nextInt(ConstantNumber.SCREEN_WIDTH),
				ConstantNumber.SCREEN_HEIGHT,
				waterMelons.get(0).getHeight() / 2, 2, -1300+random.nextInt(100),
				waterMelons, fruitBitmaps.get(1), fruitBitmaps.get(2),
				jamBitmaps, activity);
		Movable fruit1 = new Movable(
				random.nextInt(ConstantNumber.SCREEN_WIDTH),
				ConstantNumber.SCREEN_HEIGHT,
				waterMelons.get(0).getHeight() / 2, 2, -1200+random.nextInt(100),
				waterMelons, fruitBitmaps.get(1), fruitBitmaps.get(2),
				jamBitmaps, activity);
		Movable fruit2 = new Movable(
				random.nextInt(ConstantNumber.SCREEN_WIDTH),
				ConstantNumber.SCREEN_HEIGHT,
				waterMelons.get(0).getHeight() / 2, 2, -1400+random.nextInt(100),
				waterMelons, fruitBitmaps.get(1), fruitBitmaps.get(2),
				jamBitmaps, activity);
		Movable fruit3 = new Movable(
				random.nextInt(ConstantNumber.SCREEN_WIDTH),
				ConstantNumber.SCREEN_HEIGHT,
				waterMelons.get(0).getHeight() / 2, 2, -1400+random.nextInt(100),
				waterMelons, fruitBitmaps.get(1), fruitBitmaps.get(2),
				jamBitmaps, activity);
		

		Log.i("zhou", fruit.toString());
		/*
		 * Movable fruit2 = new Movable(200, 180,
		 * fruitBitmaps.get(0).getHeight() / 2, 30, 30, fruitBitmaps.get(0));
		 */
		fruits.add(fruit);
		fruits.add(fruit1);
		fruits.add(fruit2);
		fruits.add(fruit3);
		
		// fruits.add(fruit2);

	}
//绘制刀的轨迹
	public void drawKnifePath(Canvas canvas) {
		if (path != null) {
			canvas.drawPath(path, paint);
		}

	}


	public void doDrawJam(Canvas canvas) {
		// animation.start();
	}

	public void doDraw(Canvas canvas) {
		canvas.drawBitmap(bgBitmap, 0, 0, null);

		for (Movable movable : fruits) {
			// Log.i("zhou", movable.toString());
			movable.drawSelf(canvas);
			// movable.getFruitJams().drawSelf(canvas);

			/*
			 * if (movable.isPlaySound) { Log.i("zhou", "playSound");
			 * soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);
			 * movable.isPlaySound = false; }
			 */

		}
	}

	public void judgeCutting(int finger_x, int finger_y) {

		for (Movable fruit : fruits) {
			fruit.judgeCutting(finger_x, finger_y);
		}

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		this.height = getHeight();
		this.width = getWidth();
		Log.i("zhou", height + "");
		Log.i("zhou", width + "");
		if (!drawThread.isAlive()) {
			drawThread.start();
		}

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		for (Movable fruit : fruits) {
			fruit.getFruitThread().setFlag(false);
			fruit.setFruitThread(null);
		}
		drawThread.flag = false;
		drawThread = null;

	}

	/**
	 * 游戏结束出口，切换到选项界面
	 */
	public void startSelectionView() {
		Intent intent = new Intent(activity, FruitActivity.class);

		activity.startActivity(intent);
	}

	public boolean isGameOver() {
		return isGameOver;
	}

	public void setGameOver(boolean isGameOver) {
		this.isGameOver = isGameOver;
	}



	

	public Path getPath() {
		return path;
	}

	public void setPath(Path path) {
		this.path = path;
	}

}
